//
//  UIView+JSHandler.m
//  ShopMobile
//
//  Created by mac on 17/10/18.
//  Copyright © 2017年 json. All rights reserved.
//

#import "UIView+JSHandler.h"

@implementation UIView (JSHandler)
/** - 判断一个控件是否真正显示在主窗口 - */
- (BOOL)isShowingOnKeyWindow {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    // 将控件的相对于它父视图的frame转换为相对于wind的frame
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBouns = keyWindow.bounds;
    // 判断两个frame是否相交
    BOOL Intersects = CGRectIntersectsRect(newFrame, winBouns);
    // 没有隐藏 && 能看见 && 是主窗口的子控件 && 有相交
    return !self.hidden && self.alpha > 0.01 && self.window == keyWindow && Intersects;
}

- (void)setJs_x:(CGFloat)js_x {
    
    CGRect frame = self.frame;
    frame.origin.x = js_x;
    self.frame = frame;
}
- (void)setJs_y:(CGFloat)js_y {
    
    CGRect frame = self.frame;
    frame.origin.y = js_y;
    self.frame = frame;
}
- (void)setJs_width:(CGFloat)js_width {
    
    CGRect frame = self.frame;
    frame.size.width = js_width;
    self.frame = frame;
}
- (void)setJs_height:(CGFloat)js_height {
    
    CGRect frame = self.frame;
    frame.size.height = js_height;
    self.frame = frame;
}

- (CGFloat)js_x {
    return self.frame.origin.x;
}
- (CGFloat)js_y {
    return self.frame.origin.y;
    
}
- (CGFloat)js_width {
    return self.frame.size.width;
}
- (CGFloat)js_height {
    return self.frame.size.height;
}
- (void)setJs_size:(CGSize)js_size {
    CGRect frema = self.frame;
    frema.size = js_size;
    self.frame = frema;
}
- (CGSize)js_size {
    return self.frame.size;
}
- (CGFloat)js_centerX {
    return self.center.x;
}
- (void)setJs_centerX:(CGFloat)js_centerX {
    CGPoint center = self.center;
    center.x = js_centerX;
    self.center = center;
}
- (CGFloat)js_centerY {
    return self.center.y;
}
- (void)setJs_centerY:(CGFloat)js_centerY {
    CGPoint center = self.center;
    center.y = js_centerY;
    self.center = center;
}
- (CGFloat)left_js {
    return self.frame.origin.x;
}

- (void)setLeft_js:(CGFloat)left_js{
    CGRect frame = self.frame;
    frame.origin.x = left_js;
    self.frame = frame;
}

- (CGFloat)top_js {
    return self.frame.origin.y;
}

- (void)setTop_js:(CGFloat)top_js{
    CGRect frame = self.frame;
    frame.origin.y = top_js;
    self.frame = frame;
}

- (CGFloat)right_js {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight_js:(CGFloat)right_js{
    CGRect frame = self.frame;
    frame.origin.x = right_js - frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom_js {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom_js:(CGFloat)bottom_js {
    CGRect frame = self.frame;
    frame.origin.y = bottom_js - frame.size.height;
    self.frame = frame;
    
}

- (void)js_addSubviews:(NSArray *)subviews
{
    [subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        if ([view isKindOfClass:[UIView class]]) {
            [self addSubview:view];
        }
    }];
}

-(dispatch_block_t)js_RandomColor{
    return ^(){
        self.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        
    };
}
- (void)setJs_borderColor:(UIColor *)js_borderColor{
    self.layer.borderColor = js_borderColor.CGColor;
}
- (void)setJs_borderWidth:(CGFloat)js_borderWidth{
    self.layer.borderWidth = js_borderWidth;
}
- (void)setJs_cornerRadius:(CGFloat)js_cornerRadius{
    self.layer.cornerRadius = js_cornerRadius;
}
@end
